// pages/Order/Order.js
var url = wx.getStorageSync('url') 
var shopid = wx.getStorageSync('shopid')
var userId = wx.getStorageSync('userId') 
var openid = wx.getStorageSync('openid')
var time_ss = wx.getStorageSync('time_ss')

var idx ='';
var pageIndex = 1;
var list
Page({

  /**
   * 页面的初始数据
   */
  data: {
      list_s1:[{
          massge:'全部'
      }, {
          massge: '待付款'
        }, {
          massge: '待发货'
        }, {
          massge: '待收货'
        },{
          massge: '已完成'
        }, {
          massge: '已取消'
        }],
    currentTab:'0',
    "shujulist":[]
    // hiddenName:true,
  },

 
  // 获取订单
  goto_orders: function (pageIndex, idx){
    var that = this
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    console.log(userId)
    console.log(shopid)
    console.log(openid)

    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: url+'gateOrder/searchOrderByUser',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "userId": userId,
          "shopId": shopid,
          "status": idx,
          "activityType": '',
          "pageIndex": pageIndex,
          "pageSize": 1000

        }
      },
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        var data = e.data.content
        console.log(data)
        if (e.data.content.dataList.length == 0) {
          that.setData({
            shows: true,
            order: data.dataList
          })
        } else {
          that.setData({
            shows: false,
            order: data.dataList
          })
        }
        pageIndex++;
        wx.hideLoading();

      }
    })
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.goto_orders(1)
  },
  // 切换订单
  swichNav:function(e){
    
    // console.log(e.currentTarget.dataset.current)
     idx = e.currentTarget.dataset.current;
    console.log(idx)
    if(idx==0){
      idx = ''
    }else{
      idx=idx+1
    }
    var that = this
    that.setData({
      currentTab: e.currentTarget.dataset.current,
    })
    wx.request({
      url: url+'gateOrder/searchOrderByUser',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "userId": userId,
          "shopId": shopid,
          "status": idx,
          "activityType": " ",
          "pageIndex": 1,
          "pageSize": 1000

        }
      },
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        wx.hideLoading()
        var data = e.data.content
        if (e.data.content.dataList.length == 0){
          that.setData({
            shows: true,
            order: data.dataList
          })
        }else{
          that.setData({
            shows: false,
            order: data.dataList
          })
        }
        
      }
    })
  },
  // 查看物流
  goto_logistics: function (e) {
    var id = e.currentTarget.id
    console.log(e)
    wx.navigateTo({
      url: 'logistics/logistics?id='+id,
    })
  },
  // 更改状态-发货-付款-收货
  goto_Delivergoods:function(e){
    // console.log(this.data.order)
    var datas = this.data.order
    var id = e.currentTarget.id
    var status = e.currentTarget.dataset.status+1
    var idx = e.currentTarget.dataset.idx
    var that = this
    var balance = e.currentTarget.dataset.balance
    // console.log(balance)
    // wx.showLoading({
    //   title: '加载中',
    // })
    var i = e.currentTarget.dataset.status
    if(i==2){
      // 发起支付
      wx.request({
        url: url +'gateCash/payByOrder',
        data: {
          "list": false,
          "nodeId": "kdslfw",
          "platform": 2,
          "privId": "default",
          "resId": "default",
          "sendTime": time_ss,
          "seq": 1,
          "shopId": shopid,
          "source": 11,
          "sysVer": "1.0.0",
          "token": "kdsjfoewi12ijsldks",
          "type": 1,
          "userId": userId,
          "userType": 1,
          "content": {
            "shopId": shopid,
            "openId": openid,
            "appId": "wx6ca4bda138687ed8",
            "orderId":id
          }
        },
        method: "POST",
        header: { "Content-Type": "application/json" },
        success: function (e) {
          var s = e.data.result.code.substring(4)
          var i_ = e.data.result.desc
            if(s =='9999'){  //余额不足调用微信接口
              // wx.request({
              //   url: url + 'gateCash/rechargeStart',
              //   data: {
              //     "list": false,
              //     "nodeId": "kdslfw",
              //     "platform": 2,
              //     "privId": "default",
              //     "resId": "default",
              //     "sendTime": "2019-06-27 10:07:55",
              //     "seq": 1,
              //     "shopId": "0601f14722a232c1442b811c72877f88db26",
              //     "source": 11,
              //     "sysVer": "1.0.0",
              //     "token": "kdsjfoewi12ijsldks",
              //     "type": 1,
              //     "userId": "0201beaa92dd1f1e4d58b5b2b54af559c3b8",
              //     "userType": 1,
              //     "content": {
              //       "shopId": "0601f14722a232c1442b811c72877f88db26",
              //       "openId": openid,
              //       "appId": "wx6ca4bda138687ed8",
              //       "orderId": id
              //     }
              //   },
              //   method: "POST",
              //   header: { "Content-Type": "application/JSON" },
              //   success: function (e) {
                  console.log(e.data.content)
                  console.log(e)
                  var i = e.data.content
                  wx.requestPayment({
                    timeStamp: i.timeStamp,
                    nonceStr: i.nonce_str,
                    package: 'prepay_id=' + i.prepay_id,
                    signType: 'MD5',
                    paySign: i.paySign,
                    success(res) {
                     
                      wx.request({
                        url: url + 'gateCash/payByWxSuccess',
                        data: {
                          "list": false,
                          "nodeId": "kdslfw",
                          "platform": 2,
                          "privId": "default",
                          "resId": "default",
                          "sendTime": time_ss,
                          "seq": 1,
                          "shopId": shopid,
                          "source": 11,
                          "sysVer": "1.0.0",
                          "token": "kdsjfoewi12ijsldks",
                          "type": 1,
                          "userId": userId,
                          "userType": 1,
                          "content": {
                            "tradeId": i.tradeId
                          },
                          "page": 1,
                          "limit": 15
                        },
                        method: "POST",
                        header: { "Content-Type": "application/JSON" },
                        success: function (e) {
                          // 更改状态
                          wx.request({
                            url: url + 'gateOrder/updateOrderStatus',
                            data: {
                              "list": false,
                              "nodeId": "kdslfw",
                              "platform": 2,
                              "privId": "default",
                              "resId": "default",
                              "sendTime": time_ss,
                              "seq": 1,
                              "shopId": shopid,
                              "source": 11,
                              "sysVer": "1.0.0",
                              "token": "kdsjfoewi12ijsldks",
                              "type": 1,
                              "userId": userId,
                              "userType": 1,
                              "content": {
                                "id": id,
                                "status": status
                              }
                            },
                            method: "POST",
                            header: { "Content-Type": "application/json" },
                            success: function (e) {
                              // wx.hideLoading()
                              datas.splice(idx, 1);
                              var len = datas.length;
                              console.log(len)
                              //通过判断数组的长度来决定是否显示隐藏的部分
                              if (len == 0) {
                                that.setData({
                                  shows: true
                                })
                              } else {
                                that.setData({
                                  shows: false
                                })
                              };
                              //修改整天数据
                              that.setData({
                                // shows: this.data.shows,
                                order: datas
                              });
                              wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                                duration: 1000
                              })
                            }
                          })
                        }
                      })
                    },
                    fail(res) {
                      console.log(res)
                    }
                  })
                
              // })
            }else if(s == '0000'){   //余额支付  更改状态            
              wx.request({
                url: url +'gateOrder/updateOrderStatus',
                data: {
                  "list": false,
                  "nodeId": "kdslfw",
                  "platform": 2,
                  "privId": "default",
                  "resId": "default",
                  "sendTime": time_ss,
                  "seq": 1,
                  "shopId": shopid,
                  "source": 11,
                  "sysVer": "1.0.0",
                  "token": "kdsjfoewi12ijsldks",
                  "type": 1,
                  "userId": userId,
                  "userType": 1,
                  "content": {
                    "id": id,
                    "status": status
                  }
                },
                method: "POST",
                header: { "Content-Type": "application/json" },
                success: function (e) {
                  // wx.hideLoading()
                  datas.splice(idx, 1);
                  var len = datas.length;
                  console.log(len)
                  //通过判断数组的长度来决定是否显示隐藏的部分
                  if (len == 0) {
                    that.setData({
                      shows : true
                    })                   
                  } else {
                    that.setData({
                      shows : false
                    })
                  };
                  //修改整天数据
                  that.setData({
                    // shows: this.data.shows,
                    order: datas
                  });
                  wx.showToast({
                    title: '支付成功',
                    icon: 'success',
                    duration: 1000
                  })
                }
              })
            }else{
              wx.showLoading({
                title: i_,
              });
              setTimeout(function () {
                wx.hideLoading()
              }, 1000)

            }
        }
      })
    }else{
      if (i == 3) {
        wx.showToast({
          title: '提醒发货成功',
          icon: 'success',
          duration: 1000
        })
      } else {
        wx.request({
          url: url + 'gateOrder/updateOrderStatus',
          data: {
            "list": false,
            "nodeId": "kdslfw",
            "platform": 2,
            "privId": "default",
            "resId": "default",
            "sendTime": time_ss,
            "seq": 1,
            "shopId": shopid,
            "source": 11,
            "sysVer": "1.0.0",
            "token": "kdsjfoewi12ijsldks",
            "type": 1,
            "userId": userId,
            "userType": 1,
            "content": {
              "id": id,
              "status": status
            }
          },
          method: "POST",
          header: { "Content-Type": "application/json" },
          success: function (e) {
            // wx.hideLoading()
            datas.splice(idx, 1);
            var len = datas.length;
            console.log(len)
            //通过判断数组的长度来决定是否显示隐藏的部分
            if (len == 0) {
              that.setData({
                shows: true
              })
            } else {
              that.setData({
                shows: false
              })
            };
            //修改整天数据
            that.setData({
              order: datas
            });
            wx.navigateTo({
              url: 'Sign/Sign?id=' + id,
            })

          }
        })
        
      }
      
    }  
  },
  // 删除订单
  shanchu_orders:function(e){
    var that = this
    var datas = this.data.order
    var id = e.currentTarget.id;
    var idx = e.currentTarget.dataset.idx
    console.log(id)
    console.log(idx)

    wx.request({
      url: url+'gateOrder/delOrder',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "id": id
        }
      },
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
      console.log(e)
        datas.splice(idx, 1);
        console.log(datas)
        var len = datas.length;
        console.log(len)
        //通过判断数组的长度来决定是否显示隐藏的部分
        if (len == 0) {
          that.setData({
            shows: true
          })
        } else {
          that.setData({
            shows: false
          })
        };
        //修改整天数据
        that.setData({
          order: datas
        });
      }
    })
  },
  // 取消订单
  quxiao_orders:function(e){
    var that = this
    var datas = this.data.order
    var id = e.currentTarget.id;
    var idx = e.currentTarget.dataset.idx
    wx.request({
      url: url+'gateOrder/updateOrderStatus',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "id": id,
          "status": "6"
        }
      },
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        console.log(e)
        datas.splice(idx, 1);
        console.log(datas)
        var len = datas.length;
        console.log(len)
        //通过判断数组的长度来决定是否显示隐藏的部分
        if (len == 0) {
          that.setData({
            shows: true
          })
        } else {
          that.setData({
            shows: false
          })
        };
        //修改整天数据
        that.setData({
          order: datas
        });
      }
    })
  },
  // 查看订单详情
  goto_Orderdetails:function(e){
    let id = e.currentTarget.id
    wx.navigateTo({
      url: 'OrderAddress/Orderdetails/Orderdetails?order_id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData && !this.data.isLoading) {
      this.setData({ pageIndex: this.data.pageIndex + 1, isLoading: true })
      this.getList(this.data.pageIndex,idx)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
    }
    return {
      title: '转发',
      path: "pages/index/BeautyKilling/kill_details/collection/collection",
      success: function (res) {
        console.log('成功', res)
      }
    }
  }
})